package com.example.student1.service.impl;

import com.example.student1.dao.StudentRepository;
import com.example.student1.domain.Student;
import com.example.student1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentImplService implements StudentService {

        @Autowired
        StudentRepository studentRepository;

        /*查询所有学生*/
        public List<Student> findAll() {

            return studentRepository.findAll();
        }

    public Page<Student> findbyname(String name,Pageable pageable) {
        return studentRepository.findByName(name,pageable);
    }

        /*根据page进行查询*/
        public  Page<Student> findAll(Pageable pageable) {

           return studentRepository.findAll(pageable);
         }

         /*根据page和example进行查询*/
         public Page<Student>findbyexample(Example<Student> student, Pageable pageable){
             return studentRepository.findAll(student,pageable);
    }

    /*插入学生*/
        public Student insert(Student student) {
            return studentRepository.save(student);
        }

        /*更新学生*/
        public Student update(Student student) {
            return studentRepository.save(student);
        }

        /*获取学生根据id*/
        public Student getById(Long id) {
            Student student = studentRepository.findById(id).orElse(null);
            return student;
        }
        /*删除学生*/
        public void delete(Student student) {
            studentRepository.delete(student);
        }

        /*查找学生*/
        public void find(Student student) {
            studentRepository.findAll();
        }
        /*根据姓查询*/
        public List<Student> findbyname(String name) {
            return studentRepository.findByNameLike("%"+name+"%");
        }

      /*  public List<Student> findByNameAndPassword(String name,String password){
            return studentRepository.findByNameAndPassword(name,password);
        }*/

        /*根据id删除*/
        public void deletbyid(Long id)
        {
            Student student = new Student();
            student.setId(id);
            studentRepository.delete(student);
        }
    }
